package com.example.ruiji.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.ruiji.common.CustomException;
import com.example.ruiji.dto.SetmealDto;
import com.example.ruiji.mapper.SetmealMapper;
import com.example.ruiji.pojo.*;
import com.example.ruiji.service.CategoryService;
import com.example.ruiji.service.SetmealDishService;
import com.example.ruiji.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CategoryService categoryService;
    @Value("${reggie.path}")
    private String basePath;

    @Override
    @Transactional
    ///新增套餐
    public void saveWithDish(SetmealDto setmealDto) {
     ///先给套餐表给值
        this.save(setmealDto);
        //再进行数据封装 套餐商品关联表
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //给这个表的套餐字段给值
        setmealDishes.stream().map( (item)->
                {
                    item.setSetmealId(setmealDto.getId());
                    return item;
                }
                ).collect(Collectors.toList());
        //给商品关联表给值
        setmealDishService.saveBatch(setmealDishes);
    }
///分页查询
    @Override
    public R<Page> pageg(int page, int pageSize, String name) {
        //返回的是一个泛型类 --类中将套餐id转换为名字
        Page<Setmeal> pageinof=new Page<>(page,pageSize);
        Page<SetmealDto> dtoPage=new Page<>();

        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(name!=null,Setmeal::getName,name);
        //查询
        lambdaQueryWrapper.orderByDesc(Setmeal::getUpdateTime);
        this.page(pageinof,lambdaQueryWrapper);

        ///pageinof 已经含有了全部值 --但是我们要给其中的id数据抽象出来

        BeanUtils.copyProperties(pageinof,dtoPage,"records");
        List<Setmeal> records=pageinof.getRecords();//接参 准备改造
        List<SetmealDto> list=records.stream().map(
                (item)->{
                    SetmealDto setmealDto = new SetmealDto();
                    BeanUtils.copyProperties(item,setmealDto);
                    //根据分类ID来查询
                    Category category = categoryService.getById(setmealDto.getCategoryId());
                    if(category!=null)
                    {
                        String catname=category.getName();
                        setmealDto.setCategoryName((catname));
                    }
                    return  setmealDto;

                }
        ).collect(Collectors.toList());
        dtoPage.setRecords(list);
        return R.success(dtoPage);
    }

    @Override
    @Transactional
    public void delet(List<Long> ids) {
        //集体删除
        //先删除套餐表 再删除关系表中的数据

        //1.套餐删除
        LambdaQueryWrapper<Setmeal> queryWrapper=new LambdaQueryWrapper();
        //套餐如果起售就不能删除
        queryWrapper.in(Setmeal::getId,ids);
        queryWrapper.eq(Setmeal::getStatus,1);
        int count=this.count(queryWrapper);
        if(count>0)
        {
            throw new CustomException("有套餐启售,无法删除");
        }
        LambdaQueryWrapper<Setmeal> queryWrapper2=new LambdaQueryWrapper();
        queryWrapper2.in(Setmeal::getId,ids);
        List<Setmeal> list = this.list(queryWrapper2);
        this.wenjrovm(list);

        this.removeByIds(ids);

        LambdaQueryWrapper<SetmealDish> queryWrapper1=new LambdaQueryWrapper<>();
        queryWrapper1.in(SetmealDish::getSetmealId,ids);


        setmealDishService.remove(queryWrapper1);

    }

    public  void wenjrovm(List<Setmeal> list)
    {
        Iterator<Setmeal> iterator = list.iterator();
        while (iterator.hasNext())
        {
            Setmeal Setmeal = iterator.next();
            String filePath = basePath+Setmeal.getImage(); // 替换为实际的文件路径
// 创建文件对象
            File file = new File(filePath);
// 检查文件是否存在，并执行删除操作
            if (file.exists()) {
                if (file.delete()) {
                    // 文件删除成功
                    log.info("删除成功");
                } else {
                    // 文件删除失败
                    log.info("删除失败");
                }
            } else {
                // 文件不存在
                log.info("文件不存在");
            }
        }

    }

}
